package com.example.fbsmobile.util;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import android.content.Context;
import android.content.ContextWrapper;


public final class SessionInfo  extends ContextWrapper{

	private static final String filename = "FbsMobile.txt";
	private String loginResult;
	private String userId;
	private String userRole;
	
	public SessionInfo(Context base){
		super(base);
	}

	public String getLoginResult() {
		toRead();
		return loginResult;
	}

	public String getUserId() {
		toRead();
		return userId;
	}

	public String getUserRole() {
		toRead();
		return userRole;
	}

	public static String getFilename() {
		return filename;
	}
	
	public void toWrite(String contents){
		try{
			FileOutputStream f = openFileOutput(filename,Context.MODE_PRIVATE);
			PrintWriter w = new PrintWriter(f);
			w.println(contents);
			w.close();
			f.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	private void toRead() {
		try{
			FileInputStream fos = openFileInput(filename);
			BufferedReader r = new BufferedReader(new InputStreamReader(fos));
			String l;
			while((l = r.readLine()) != null){
				loginResult = l;
				userId = r.readLine();
				userRole = r.readLine();
			}r.close();
			fos.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}
